package com.sxbbc.common.core.service.impl;

import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.ProcEvaluate;
import com.sxbbc.common.core.entity.jz.ProcMain;
import com.sxbbc.common.core.mapper.jz.ProcEvaluateMapper;
import com.sxbbc.common.core.mapper.jz.ProcMainMapper;
import com.sxbbc.common.core.service.IProcEvaluateService;
import com.sxbbc.common.core.utils.BasePage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 商品评论表 服务实现类
 * </p>
 *
 * @author xiaoky
 * @since 2019-05-29
 */
@Service
public class ProcEvaluateServiceImpl extends ServiceImpl<ProcEvaluateMapper, ProcEvaluate> implements IProcEvaluateService {
	@Autowired
	private ProcMainMapper procMainMapper;
	@Autowired
	private ProcEvaluateMapper procEvaluateMapper;
	
	@Override
	public Map<String, Object> getProEvaluateNum(String proCode) {
		Integer total = 0;
		Integer hasPhoto = 0;
		// 通过pcode查询商品信息,暂时先用这个查询
		QueryWrapper<ProcMain> wrapper = new QueryWrapper<ProcMain>();
		wrapper.eq("pro_code", proCode);
		ProcMain  proc = procMainMapper.selectOne(wrapper);
		if (proc != null) {
			// 全部评论数
			QueryWrapper<ProcEvaluate> evaluateWrapper = new QueryWrapper<ProcEvaluate>();
			evaluateWrapper.eq("pro_id", proc.getId());
			total = super.count(evaluateWrapper);
			// 有图的评论数
			evaluateWrapper.eq("has_photo", StaticUtils.STATUS_YES);
			hasPhoto = super.count(evaluateWrapper);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("hasPhoto", hasPhoto);
		return map;
	}

	@Override
	public IPage<ProcEvaluate> queryProEvaluateList(BasePage<ProcEvaluate> page, String proCode, Integer hasPhoto) {
		CalcuUtils calcuUtils = CalcuUtils.getInstance();
		// 通过pcode查询商品信息,暂时先用这个查询
		QueryWrapper<ProcMain> wrapper = new QueryWrapper<ProcMain>();
		wrapper.eq("pro_code", proCode);
		ProcMain  proc = procMainMapper.selectOne(wrapper);
		if (proc != null) {
			IPage<ProcEvaluate> pageInfo = procEvaluateMapper.queryEvaluateByProCode(page, proc.getId(), hasPhoto);
			for (ProcEvaluate evaluate : pageInfo.getRecords()) {
				if (calcuUtils.isEquals(evaluate.getHasPhoto(), StaticUtils.STATUS_YES)
						&&!StringUtils.isEmpty(evaluate.getEvaluateUrl())) {
					evaluate.setEvaluateUrlList(Arrays.asList(evaluate.getEvaluateUrl().split(",")));
				}
				// 是否匿名
				if (calcuUtils.isEquals(evaluate.getHide(), StaticUtils.STATUS_YES)) {
					evaluate.setCHeadUrl("");
					evaluate.setCNickName(calcuUtils.replaceHideName(evaluate.getCNickName()));
				}
			}
			return pageInfo;
		}
		return new BasePage<ProcEvaluate>();
	}

}
